﻿using System;
using System.IO;
using Z.ExtensionMethods.Object;
using Z.GenerateIconProject.WinForm.BL;

namespace Z.GenerateIconProject.WinForm
{
    public partial class GenerateProjectManager
    {
        public GenerateProjectManager(ProjectConfig config)
        {
            Config = config;
        }
        public Result IsValid()
        {
            //Dictionary<string, FileInfo> dict16 = Icon16DirectoryPath.ToDirectoryInfo().GetFiles().ToDictionary(info => info.Name);
            //Dictionary<string, FileInfo> dict32 = Icon32DirectoryPath.ToDirectoryInfo().GetFiles().ToDictionary(info => info.Name);

            //var dictionaryCompare = new DictionaryCompare<string, FileInfo, FileInfo>(dict16, dict32);
            //dictionaryCompare.Compare();

            //return dictionaryCompare.ListCompareResult;

            //   if (Icon16DirectoryPath != "" && Icon32DirectoryPath != "")
            //{
            //    ListCompareResult<KeyValuePair<string, FileInfo>, KeyValuePair<string, FileInfo>> compareResult = IsSynchro();

            //    if (compareResult.ItemListLeft.Count > 0 || compareResult.ItemListRight.Count > 0)
            //    {
            //        var sb = new StringBuilder();
            //        sb.AppendLine("Your Icon directory are not synchronized, are you sure you want to continue?");
            //        sb.AppendLine();
            //        sb.AppendLine("Icon16:");
            //        foreach (var item in compareResult.ItemListLeft)
            //        {
            //            sb.AppendLine("- " + item.Value.Name);
            //        }
            //        sb.AppendLine();
            //        sb.AppendLine("Icon32:");
            //        foreach (var item in compareResult.ItemListRight)
            //        {
            //            sb.AppendLine("- " + item.Value.Name);
            //        }
            //        DialogResult sResult = MessageBox.Show(sb.ToString(), "Directory not synchronized", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            //        if (sResult == DialogResult.No)
            //        {

            //            return false;
            //        }
            //    }
            return true;
        }

        public void EnsureDirectoryExists()
        {
            Directory.CreateDirectory(Config.GeneratedSupportFilePath);
            Directory.CreateDirectory(Config.GeneratedProjectFilePath);
        }

        public void CleanDirectory()
        {
            Config.GeneratedSupportFilePath.ToDirectoryInfo().Clear();
            Config.GeneratedProjectFilePath.ToDirectoryInfo().Clear();
        }

        public void GenerateSupportFile()
        {
            var dir = My.TemplateFile.SupportFile;
            var dirOutput = Config.GeneratedSupportFilePath;

            foreach (var file in dir.GetFiles("*.*", SearchOption.AllDirectories))
            {
                var template = file.ReadToEnd();

                template = template.Replace("@(Model.Namespace)", Config.Namespace);
                template = template.Replace("@(Model.HandlerPath)", Config.Namespace + ".axd");

                var output = file.FullName.Replace(dir.FullName, dirOutput).ToFileInfo();
                output = Path.Combine(output.Directory.FullName, output.GetFileNameWithoutExtension()).ToFileInfo();
                Directory.CreateDirectory(output.Directory.FullName);
                template.SaveAs(output);
            }
        }

        public void GenerateSupportIconFile()
        {
            
        }

        public void GenerateProjectFile()
        {
            var dir = My.TemplateFile.ProjectFile;
            var dirOutput = Config.GeneratedProjectFilePath;

            var iconEnum = IconLibraryHelper.GetEnumText(Config);
            var css = IconLibraryHelper.GetCssText(Config);
            var embedded = IconLibraryHelper.GetEmbeddedText(Config);
            var projectembedded = IconLibraryHelper.GetProjectEmbeddedText(Config);
            var includeExtensions = IconLibraryHelper.GetIncludeExtensionsText(Config);
            string frameworkVersion = Config.IsLegacy ? "v2.0" : "v3.5";
            string namespaceStart = Config.IsLegacy ? "namespace " + Config.Namespace + "{" : "";
            string namespaceEnd = Config.IsLegacy ? "}" : "";
            string CssFileName = Config.ProjectName + ".css";
            string HandlerPath = Config.Namespace + ".axd";
            string Guid = System.Guid.NewGuid().ToString();
            string sourceControl = @"
    <SccProjectName>SAK</SccProjectName>
    <SccLocalPath>SAK</SccLocalPath>
    <SccAuxPath>SAK</SccAuxPath>
    <SccProvider>SAK</SccProvider>
";

            foreach (var file in dir.GetFiles("*.*", SearchOption.AllDirectories))
            {
                var template = file.ReadToEnd();

                template = template.Replace("@(Model.SourceControl)", sourceControl);
                template = template.Replace("@(Model.Namespace)", Config.Namespace);
                template = template.Replace("@(Model.HandlerPath)", HandlerPath);
                template = template.Replace("@(Model.CssFileName)", CssFileName);
                template = template.Replace("@(Model.IncludeExtensions)", includeExtensions);
                template = template.Replace("@(Model.CssFileNameDebug)", CssFileName.Replace(".css", ".debug.css"));
                template = template.Replace("@(Model.IconEnum)", iconEnum);
                template = template.Replace("@(Model.Css)", css);
                template = template.Replace("@(Model.Embedded)", embedded);
                template = template.Replace("@(Model.ProjectEmbedded)", projectembedded);
                template = template.Replace("@(Model.FrameworkVersion)", frameworkVersion);
                template = template.Replace("@(Model.NamespaceStart)", namespaceStart);
                template = template.Replace("@(Model.NamespaceEnd)", namespaceEnd);
                template = template.Replace("@(Model.LicenseComment)", Config.License);
                template = template.Replace("@(Model.Guid)", Guid);
                template = template.Replace("@(Model.ExtensionMethod)", Config.IsLegacy ? "" : "this ");

                var newFileName = file.FullName.Replace("@(Model.Namespace)", Config.Namespace);
                newFileName = newFileName.Replace("@(Model.CssFileName)", CssFileName);
                newFileName = newFileName.Replace("@(Model.ProjectName)", Config.ProjectName);

                var output = newFileName.Replace(dir.FullName, dirOutput).ToFileInfo();
                output = Path.Combine(output.Directory.FullName, output.GetFileNameWithoutExtension()).ToFileInfo();
                Directory.CreateDirectory(output.Directory.FullName);
                template.SaveAs(output);
            }
        }

        public void GenerateProjectIconFile()
        {
            var dir = My.TemplateFile.ProjectIconFile;
            var dirOutput = Config.GeneratedProjectFilePath;


            foreach (var icon in Config.IconConfigs)
            {
                string frameworkVersion = Config.IsLegacy ? "v2.0" : "v3.5";
                string namespaceStart = Config.IsLegacy ? "namespace " + Config.Namespace + "{" : "";
                string namespaceEnd = Config.IsLegacy ? "}" : "";
                string CssFileName = "";
                string HandlerPath = Config.Namespace + ".axd";

                foreach (var file in dir.GetFiles("*.*", SearchOption.AllDirectories))
                {
                    var template = file.ReadToEnd();

                    template = template.Replace("@(Model.ProjectName)", Config.ProjectName);
                    template = template.Replace("@(Model.Namespace)", Config.Namespace);
                    template = template.Replace("@(Model.HandlerPath)", HandlerPath);
                    template = template.Replace("@(Model.CssFileName)", CssFileName);
                    template = template.Replace("@(Model.CssFileNameDebug)", CssFileName.Replace(".css", ".debug.css"));
                    template = template.Replace("@(Model.FrameworkVersion)", frameworkVersion);
                    template = template.Replace("@(Model.NamespaceStart)", namespaceStart);
                    template = template.Replace("@(Model.NamespaceEnd)", namespaceEnd);
                    template = template.Replace("@(Model.LicenseComment)", Config.License);
                    template = template.Replace("@(Model.ExtensionMethod)", Config.IsLegacy ? "" : "this ");
                    template = template.Replace("@(Model.DestinationName)", icon.DestinationName);
                    template = template.Replace("@(Model.Suffix)", icon.Suffix);

                    var newFileName = file.FullName.Replace("@(Model.Namespace)", Config.Namespace);
                    newFileName = newFileName.Replace("@(Model.DestinationName)", icon.DestinationName);

                    var output = newFileName.Replace(dir.FullName, dirOutput).ToFileInfo();
                    output = Path.Combine(output.Directory.FullName, output.GetFileNameWithoutExtension()).ToFileInfo();
                    Directory.CreateDirectory(output.Directory.FullName);
                    template.SaveAs(output);
                }
            }
        }

        public void CopyIcon()
        {
            var dirOutput = Config.GeneratedProjectFilePath;

            foreach (var icon in Config.IconConfigs)
            {
                Directory.CreateDirectory(Path.Combine(dirOutput, icon.DestinationName));
                foreach (var file in icon.SourcePath.ToDirectoryInfo().GetFiles())
                {
                    var output = Path.Combine(dirOutput, icon.DestinationName, file.Name);
                    file.CopyTo(output);
                }
            }
        }
    }
}